package one.bugu.android.demon.ui.adapter.shop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.MagicCalShopBean;
import one.bugu.android.demon.ui.activity.shop.OrderAffirmActivity;
import one.bugu.android.demon.ui.widget.CustomListAdapter;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.MyTextUtils;
import one.bugu.android.demon.util.OnViewClickListener;
import one.bugu.android.demon.util.ParamUtils;

/**
 * 魔法算力商店适配器
 */
public class MagicCalShopAdapter extends CustomListAdapter<MagicCalShopBean> {

    private Context mContext;

    private DecimalFormat df = new DecimalFormat("###");

    public MagicCalShopAdapter(Context context, ArrayList<MagicCalShopBean> objList) {
        super(context, objList);
        this.mContext = context;
    }

    @Override
    protected View lpgetView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_magic_cal_shop, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        if (mObjList != null) {
            setListData(mHolder, position);
        }
        return convertView;
    }

    /**
     * 填充列表数据
     *
     * @param mHolder
     * @param position
     */
    @SuppressLint("SetTextI18n")
    private void setListData(ViewHolder mHolder, int position) {
        final MagicCalShopBean magicCalShopBean = mObjList.get(position);
        mHolder.tv_mgc_give.setText("首冲额外赠送" + df.format(magicCalShopBean.getFirstGiveBgtNum()) + "BGT");
        mHolder.tv_mgc_title.setText(df.format(magicCalShopBean.getPowerNum()) + "魔法算力  送" + df.format(magicCalShopBean.getGiveBgtNum()) + "BGT");
        SpannableString text = MyTextUtils.getInstance().setAutoTextColor("有效期" + magicCalShopBean.getValidTime() + "天", "[0-9]{1,}", "#FF5958");
        mHolder.tv_mgc_indate.setText(text);

        //购买按钮点击事件
        mHolder.tv_mgc_buy.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                Intent intent = new Intent(mContext, OrderAffirmActivity.class);
                intent.putExtra("MGC_DATA", magicCalShopBean);
                mContext.startActivity(intent);
            }
        });
    }

    public static class ViewHolder {
        public View rootView;
        public ImageView iv_mgc_icon;
        public TextView tv_mgc_give;
        public TextView tv_mgc_title;
        public TextView tv_mgc_indate;
        public TextView tv_mgc_buy;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_mgc_icon = (ImageView) rootView.findViewById(R.id.iv_mgc_icon);
            this.tv_mgc_give = (TextView) rootView.findViewById(R.id.tv_mgc_give);
            this.tv_mgc_title = (TextView) rootView.findViewById(R.id.tv_mgc_title);
            this.tv_mgc_indate = (TextView) rootView.findViewById(R.id.tv_mgc_indate);
            this.tv_mgc_buy = (TextView) rootView.findViewById(R.id.tv_mgc_buy);
        }

    }
}
